package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CateMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.SetmealService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Resource
    private SetmealMapper setmealMapperl;

    @Resource
    private SetmealDishMapper setmealDishMapper;

    @Resource
    private CateMapper cateMapper;

    @Override
    public void add(Setmeal setmeal) {
        // 新增套餐
        setmealMapperl.insert(setmeal);

        // 绑定关联的菜品
        for (SetmealDish sd : setmeal.getSetmealDishes()) {
            // 给每个菜品绑定对应的套餐
            sd.setSetmealId(setmeal.getId());

            // 新增菜品
            setmealDishMapper.insert(sd);
        }
    }

    @Override
    public void edit(Setmeal setmeal) {
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();

        // 修改套餐
        setmealMapperl.updateById(setmeal);

        // 删除之前绑定的所有菜品
        setmealDishMapper.deleteBySetmealId(setmeal.getId());

        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            // 批量新增菜品
            for (SetmealDish sd : setmealDishes) {
                sd.setSetmealId(setmeal.getId());
            }

            // 批量新增菜品
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    @Override
    public void del(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapperl.selectById(id);

            // 如果是起售状态，不允许被删除
            if (StatusConstant.ENABLE.equals(setmeal.getStatus())) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }

            // 删除套餐
            setmealMapperl.deleteById(id);

            // 删除绑定的菜品
            setmealDishMapper.deleteBySetmealId(id);
        }
    }

    @Override
    public Page<Setmeal> page(SetmealPageQueryDTO s) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();

        if (s.getName() != null && !s.getName().isEmpty()) {
            queryWrapper.like("name", "%" + s.getName() + "%");
        }

        if (s.getCategoryId() != null && s.getCategoryId() > 0) {
            queryWrapper.eq("category_id", s.getCategoryId());
        }

        if (s.getStatus() != null) {
            queryWrapper.eq("status", s.getStatus());
        }

        Page<Setmeal> page = new Page<>(s.getPage(), s.getPageSize());

        Page<Setmeal> res = setmealMapperl.selectPage(page, queryWrapper);

        // 关联分类名称
        for (Setmeal setmeal : res.getRecords()) {
            Category category = cateMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());
        }

        return res;
    }
}
